<template>
  <div class="floating-chat-container">
    <!-- 浮动聊天图标 -->
    <div 
      v-if="!isChatOpen" 
      class="floating-chat-icon"
      @click="openChat"
      :class="{ 'pulse': hasUnreadMessages }"
    >
      <el-icon class="chat-icon">
        <ChatDotRound />
      </el-icon>
      <!-- 未读消息提示 -->
      <div v-if="unreadCount > 0" class="unread-badge">
        {{ unreadCount > 99 ? '99+' : unreadCount }}
      </div>
    </div>

    <!-- 聊天对话框 -->
    <ChatDialog
      v-if="isChatOpen"
      :visible="isChatOpen"
      @close="closeChat"
      @minimize="minimizeChat"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { ChatDotRound } from '@element-plus/icons-vue'
import ChatDialog from '@/components/ChatDialog/index.vue'
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()

// 聊天状态
const isChatOpen = ref(false)
const unreadCount = ref(0)

// 计算是否有未读消息
const hasUnreadMessages = computed(() => unreadCount.value > 0)

// 打开聊天
const openChat = () => {
  isChatOpen.value = true
  // 清除未读消息计数
  unreadCount.value = 0
}

// 关闭聊天
const closeChat = () => {
  isChatOpen.value = false
}

// 最小化聊天
const minimizeChat = () => {
  isChatOpen.value = false
}

// 监听全局未读消息事件
const handleUnreadMessage = (event: CustomEvent) => {
  const { count } = event.detail || {}
  if (typeof count === 'number') {
    unreadCount.value = count
  }
}

// 监听全局消息接收事件
const handleMessageReceived = (event: CustomEvent) => {
  // 如果聊天窗口没有打开，增加未读计数
  if (!isChatOpen.value) {
    unreadCount.value++
  }
}

onMounted(() => {
  // 监听未读消息更新事件
  window.addEventListener('unread-message-updated', handleUnreadMessage as EventListener)
  // 监听消息接收事件
  window.addEventListener('message-received', handleMessageReceived as EventListener)
})

onUnmounted(() => {
  // 清理事件监听
  window.removeEventListener('unread-message-updated', handleUnreadMessage as EventListener)
  window.removeEventListener('message-received', handleMessageReceived as EventListener)
})
</script>

<style lang="scss" scoped>
.floating-chat-container {
  position: fixed;
  bottom: 10px;
  right: 20px;
  z-index: 9999;
}

.floating-chat-icon {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  transition: all 0.3s ease;
  position: relative;
  
  &:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 25px rgba(102, 126, 234, 0.6);
  }
  
  &:active {
    transform: scale(0.95);
  }
  
  &.pulse {
    animation: pulse 2s infinite;
  }
  
  .chat-icon {
    font-size: 24px;
    color: white;
  }
  
  .unread-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #ff4757;
    color: white;
    border-radius: 50%;
    min-width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    border: 2px solid white;
    box-shadow: 0 2px 8px rgba(255, 71, 87, 0.4);
  }
}

@keyframes pulse {
  0% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  }
  50% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.8), 0 0 0 10px rgba(102, 126, 234, 0.1);
  }
  100% {
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .floating-chat-container {
    bottom: 10px;
    right: 15px;
  }
  
  .floating-chat-icon {
    width: 50px;
    height: 50px;
    
    .chat-icon {
      font-size: 20px;
    }
    
    .unread-badge {
      min-width: 18px;
      height: 18px;
      font-size: 11px;
    }
  }
}
</style>
